package org.tloss.web.editor.enity.editors;

import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.core.NamedMember;
import org.eclipse.jdt.ui.IJavaElementSearchConstants;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.viewers.DialogCellEditor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.SelectionDialog;
import org.tloss.web.editor.Utils;

public class JavaClassDialogEditor extends DialogCellEditor {
	
	
	public JavaClassDialogEditor(Composite parent) {
		super(parent);
	}

	@SuppressWarnings("restriction")
	@Override
	protected Object openDialogBox(Control arg0) {
		SelectionDialog dialog;
		try {
			dialog = JavaUI.createTypeDialog(arg0.getShell(),
					new ProgressMonitorDialog(arg0.getShell()),
					Utils.getCurrentProject(),
					IJavaElementSearchConstants.CONSIDER_CLASSES, false);
			dialog.open();
			Object value = dialog.getResult();
			if (value != null && value instanceof Object[]) {
				Object[] objects = (Object[]) value;
				String text;
				try {
					text = ((NamedMember) objects[0]).getFullyQualifiedName(
							'$', false);
					return text;
				} catch (JavaModelException e1) {
					Utils.logError("Can't load java model", e1);
				}
			}

		} catch (JavaModelException e) {
			Utils.logError("Can't load java model", e);
		}
		return "";
	}
}
